#include<iostream>
#include<list>
#include<stack>
#include<string>
using namespace std;


  struct ListNode {
      int val;
      ListNode *next;
      ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) : val(x), next(next) {}
  };
 
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
    if (l1->val == 0)
        return l2;
    if (l2->val == 0)
        return l1;
    string s1;
    while (l1)
    {
        s1.push_back(l1->val+48);
        l1 = l1->next;
    }
    string s2;
    while (l2)
    {
        s2.push_back(l2->val+48);
        l2 = l2->next;
    }
    stack<int> s;
    int end1 = s1.size() - 1;
    int end2 = s2.size() - 1;
    int flag = 0;
    while (end1 >= 0 || end2 >= 0)
    {
        int n1 = end1 >= 0 ? s1[end1] - 48 : 0;
        int n2 = end2 >= 0 ? s2[end2] - 48 : 0;
        s.push((n1 + n2 + flag) % 10);
        flag = (n1 + n2 + flag) / 10;
        end1--;
        end2--;
    }
    if (flag == 1)
        s.push(1);
    ListNode* head = nullptr;
    ListNode* cur = head;
    while (!s.empty())
    {
        if (head == nullptr)
        {
            head = new ListNode(s.top());
            cur = head;
        }
        else
        {
            cur->next = new ListNode(s.top());
            cur = cur->next;
        }
        s.pop();
    }
    return head;
}

int main()
{
    ListNode* l1 = new ListNode(7);
    ListNode* l2 = new ListNode(2);
    ListNode* l3 = new ListNode(4);
    ListNode* l4 = new ListNode(3);
    ListNode* N1 = new ListNode(5);
    ListNode* N2 = new ListNode(6);
    ListNode* N3 = new ListNode(4);
    l1->next = l2;
    l2->next = l3;
    l3->next = l4;
    N1->next = N2;
    N2->next = N3;
    addTwoNumbers(l1, N1);
}